package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.entity.Capacita;
import swimv2.ejb.entity.Utente;
import swimv2.ejb.remote.GestioneAmiciRemote;
import swimv2.ejb.remote.GestioneCapacitaRemote;
import swimv2.ejb.remote.RicercaAmiciRemote;
import swimv2.ejb.util.LoginToken;
import util.HtmlUtil;

/**
 * Servlet implementation class CercaNuoviAmici
 */
public class CercaUtente extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see Servlet#Servlet()
     */
    public CercaUtente() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		showCercaUtente(request,response);
		

	}

	private void showCercaUtente(HttpServletRequest request,
			HttpServletResponse response) {
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			List<Capacita> capacita = gestioneCapacitaRemote.getListaCapacita();
			request.setAttribute("capacita", capacita);
						
			HtmlUtil.CompletaEdIncludi(request, response,"/WEB-INF/pages/cercaUtente.jsp");
			} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("richiediAmicizia"))
		{
			richiediAmicizia(request,response);
		} else if(azione.equals("cercaUtente"))
		{
			cercaUtente(request,response);
			
		}
	}

	private void cercaUtente(HttpServletRequest request,
			HttpServletResponse response) {
		String[] lista = request.getParameterValues("CapacitaCercate");
		List<Integer> elencoCapacita= new ArrayList<Integer>();
		
		if(lista != null)
			for(String s:lista)
				elencoCapacita.add(Integer.parseInt(s));
		String usernameCercato = request.getParameter("usernameCercato");
		
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("RicercaAmici/remote");
			RicercaAmiciRemote ricercaAmiciRemote =(RicercaAmiciRemote) PortableRemoteObject.narrow(ref1, RicercaAmiciRemote.class);
			List<Utente> utentiTrovati = ricercaAmiciRemote.cercaUtente(usernameCercato, elencoCapacita);
			request.setAttribute("utentiTrovati", utentiTrovati);			
			showCercaUtente(request,response);
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void richiediAmicizia(HttpServletRequest request,
			HttpServletResponse response) {
		String usernameAmico=(String) request.getParameter("usernameAmico");
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneAmici/remote");
			GestioneAmiciRemote gestioneAmiciRemote =(GestioneAmiciRemote) PortableRemoteObject.narrow(ref1, GestioneAmiciRemote.class);
			
			gestioneAmiciRemote.richiediAmico(username, usernameAmico);
			HtmlUtil.redirect(request, response, "/Amici");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
